package com.JL.engine.View;

import com.JL.engine.model.Entity;
import com.JL.engine.model.View;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class AnimationChannel <K> implements View {
    private final Map <K,AnimationView> viewMap=new HashMap<>();
    private AnimationView animationView;

    @Override
    public void draw(Graphics g, Entity entity) {
        animationView.draw(g,entity);
    }
    public void putAnimation(K key,AnimationView view){
        Objects.requireNonNull(view,"视图不能为null");
        Objects.requireNonNull(key,"key不能为null");
        viewMap.put(key, view);
    }
    public void removeAnimation(K key){
        viewMap.remove(key);
    }
    public void playerAnimationChannel(K key){
        if (!viewMap.containsKey(key)){
            throw new IllegalArgumentException("key:"+key+"not found");
        }
        animationView=viewMap.get(key);
        animationView.play();
    }
    public void loopAnimationChannel(K key,int cycleCount){
        if (!viewMap.containsKey(key)){
            throw new IllegalArgumentException("key:"+key+"not found");
        }
        animationView=viewMap.get(key);
        animationView.loop(cycleCount);
    }
    public void loopAnimationChannel(K key){this.loopAnimationChannel(key,-1);}
}
